package org.fuxia.im.common;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.Window;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

/**
 * 常用对话框组件，提供模态等待、消息提示等对话框
 * @author Leo
 *
 */
public final class CommonDialog
{
	/**
	 * 创建一个带有进度条的模态等待窗口
	 * @param parent
	 * @return
	 */
	public static final JDialog createModelWaitDialog(Window parent)
	{
		int x = parent.getX() + parent.getWidth() / 2 - 200 / 2;
		int y = parent.getY() + parent.getHeight() / 3;
		JDialog dia = new JDialog(parent, "对话框");
		dia.setBounds(x, y, 200, 20);
		dia.setLayout(new BorderLayout());
		JProgressBar progressBar = new JProgressBar(0, 100);
		progressBar.setString("请等待...");
		progressBar.setStringPainted(true);
		progressBar.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		progressBar.setIndeterminate(true);
		dia.setModal(true);
		dia.add(progressBar, BorderLayout.CENTER);
		dia.setUndecorated(true);
		return dia;
	}

	/**
	 * 创建一个消息提示窗口
	 * @param tips
	 * @return
	 */
	public static final MyMessageDialog createMessageNotifyWindow(String tips)
	{
		return new MyMessageDialog("新消息到达提示",tips);
	}
	/**
	 * 显示系统错误信息对话框
	 * @param parent
	 * @param message
	 */
	public static final void showErrorMessageDialog(final Component parent,final String message) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JOptionPane.showMessageDialog(parent, message,
						"出现错误", JOptionPane.ERROR_MESSAGE);
			}
		});
	}
	/**
	 * 显示信息对话框
	 * @param parent
	 * @param message
	 */
	public static final void showMessageDialog(final Component parent,final String message){
		if(message==null||"".equals(message)) {
			return  ;
		}
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JOptionPane.showMessageDialog(parent, message);
			}
		});
	}
}
